package com.weisha.bledemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.weisha.bledemo.application.App;
import com.weisha.bledemo.retrofit.CommonOkHttp;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.GET;

public class FirstActivity extends Activity implements View.OnClickListener{

    private Button btn_NFC, btn_bluetooth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        getActionBar().setTitle(R.string.title_first);
        initView();
    }

    /**
     * 界面初始化
     */
    public void initView(){
        btn_NFC = (Button) findViewById(R.id.btn_NFC);
        btn_bluetooth = (Button) findViewById(R.id.btn_bluetooth);

        btn_NFC.setOnClickListener(this);
        btn_bluetooth.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPswd();
    }

    public void getPswd(){
        CommonOkHttp.toGetPSWD(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccessful()){
                    ResponseBody body = response.body();
                    try {
                        App.password = body.string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println(App.password);

                }else{
                    Toast.makeText(FirstActivity.this, "服务器连接失败，请检查网络后重试！",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_NFC:
                Intent intent1 = new Intent(FirstActivity.this, MainActivity.class);
                startActivity(intent1);
                break;

            case R.id.btn_bluetooth:
//                Intent intent = new Intent(FirstActivity.this, DeviceScanActivity.class);
//                startActivity(intent);
                final Intent intent = new Intent(this, DeviceActivity.class);
                intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, "PD_0E10");
                intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, "EC:53:5E:04:0E:10");
                startActivity(intent);
                break;
        }
    }
}
